package cc.iotkit.model.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Title: cc.iotkit.model.response
 * @author: Sunne
 * @date: 2024-09-23 18:32
 * @Description: 前端响应构造体
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private Integer code;

    private T data;

    private String msg;

    private final long timestamps = System.currentTimeMillis();

    public synchronized static <T> ResponseResult<T> build(ResponseCode statusEnum) {
        return build(statusEnum,null);
    }

    public synchronized static <T> ResponseResult<T> buildMsg(ResponseCode responseCode, String msg) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(responseCode.code);
        res.setMsg(msg);
        return res;
    }

    public synchronized static <T> ResponseResult<T> buildMsg(Integer code, String msg) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(code);
        res.setMsg(msg);
        return res;
    }

    public synchronized static <T> ResponseResult<T> build(ResponseCode responseCode, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(responseCode.code);
        res.setMsg(responseCode.msg);
        res.setData(data);
        return res;
    }

    public synchronized static <T> ResponseResult<T> ok() {
        return build(ResponseCode.SUCCESS,null);
    }

    public synchronized static <T> ResponseResult<T> okHasData(T data) {
        return build(ResponseCode.SUCCESS,data);
    }


    private static final long serialVersionUID = 8992436576262574064L;
}